package com.yuntsg.ruiijn.controller.admin;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuntsg.ruiijn.controller.system.InitController;
import com.yuntsg.ruiijn.database.entity.CheckFund;
import com.yuntsg.ruiijn.database.entity.WosBasic;
import com.yuntsg.ruiijn.database.mapper.CheckFundMapper;
import com.yuntsg.ruiijn.database.mapper.WosBasicMapper;
import com.yuntsg.nnsfcp.common.beans.ResultBean;
import common.util.StringUtil;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;


@RestController
@RequestMapping("admin_basic_fund")
@Slf4j
@SuppressWarnings("ALL")
//@Api(tags = "基金规范")
public class AdminBasicFundController {
    @Autowired
    private WosBasicMapper wosBasicMapper;
    @Autowired
    private CheckFundMapper fundMapper;


    @Autowired
    private InitController initController;


    //@ApiOperation(value = "02-获取当前所有的基金列表")
    @GetMapping("all_fund")
    public ResultBean<Object> all_fund(Integer page, Integer limit) {

        Page<CheckFund> fundPage = new LambdaQueryChainWrapper<>(fundMapper).orderByDesc(CheckFund::getCount).page(new Page<>(page, limit));

        return new ResultBean<>(fundPage);
    }

    //@ApiOperation(value = "03-规范基金名称")
    @GetMapping("update_fund")
    public ResultBean<Object> update_fund(Integer id, String newName) {
        CheckFund checkFund = fundMapper.selectById(id);
        checkFund.setNewName(newName);
        fundMapper.updateById(checkFund);

        /**
         * 将规范后的数据返回给文章表
         */
        List<WosBasic> list = new ArrayList<>();
        if (checkFund.getOldName().contains("'")) {
            list = new LambdaQueryChainWrapper<>(wosBasicMapper).last(" where  JSON_CONTAINS( fund_list, JSON_ARRAY(\"" + checkFund.getOldName() + "\"))").list();
        } else {
            list = new LambdaQueryChainWrapper<>(wosBasicMapper).last(" where  JSON_CONTAINS( fund_list, JSON_ARRAY('" + checkFund.getOldName() + "'))").list();
        }
        System.out.println("获取的LIST的集合长度为___" + list.size());


        for (WosBasic wosBasic : list) {
            String fundListNew = wosBasic.getFundListNew();
            if (StringUtil.isEmpty(fundListNew)) {
                List<String> listFund = new ArrayList<>();
                listFund.add(newName);
                wosBasic.setFundListNew(JSONObject.toJSONString(listFund));
                wosBasicMapper.updateById(wosBasic);
                System.out.println("更新了数据___" + wosBasic.getId());
            } else {
                String listNew = wosBasic.getFundListNew();
                List<String> strings = JSONObject.parseArray(listNew, String.class);
                Set<String> set = new HashSet<>();
                set.addAll(strings);
                set.add(newName);
                wosBasic.setFundListNew(JSONObject.toJSONString(set));
                wosBasicMapper.updateById(wosBasic);
                System.out.println("更新了数据___" + wosBasic.getId());
            }
        }
        try {
            initController.run();
        } catch (Exception e) {
        }
        return new ResultBean<>("操作成功");
    }


}
